package com.frame.stant.utils;


import com.frame.stant.constans.ResultCode;
import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class R<T> {

    private String code;

    private String msg;

    private T data;

    public static R success(){
        return R.builder()
                .code(ResultCode.SUCCESS.getCode())
                .msg(ResultCode.SUCCESS.getMessage())
                .build();
    }

    public static R success(Object object){
        Object data = null;
        if(object instanceof Boolean){
            if( !((Boolean)object) ){
                return R.builder()
                        .code(ResultCode.SYSTEM_EXECUTION_ERROR.getCode())
                        .msg(ResultCode.SYSTEM_EXECUTION_ERROR.getMessage())
                        .build();
            }
        }else{
            data = object;
        }
        return R.builder()
                .code(ResultCode.SUCCESS.getCode())
                .msg(ResultCode.SUCCESS.getMessage())
                .data(data)
                .build();
    }

    public static R error(ResultCode resultCode){
        return R.builder()
                .code(resultCode.getCode())
                .msg(resultCode.getMessage())
                .build();
    }

    public static R error(String message){
        return R.builder()
                .code(ResultCode.SYSTEM_EXECUTION_ERROR.getCode())
                .msg(message)
                .build();
    }

}
